﻿using AutoMapper;
using B.S.BasicData.Api.Write.Applications.Command;
using B.S.BasicData.Domains;
using B.S.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace B.S.BasicData.Api.Write.Controllers
{
    /// <summary>
    /// 控制器--写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class MangerController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<MangerController> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public MangerController(IMediator mediator, ILogger<MangerController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddPermisson(AddPermissonCommand command)
        {
            try
            {
                logger.LogInformation("添加权限成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加权限失败"+ex.Message);
                throw;
            }
            
        }
        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> DelPermisson(DelPermissonCommand command)
        {
            try
            {
                logger.LogInformation("删除权限成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("删除权限失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddRole(AddRoleCommand command)
        {

            try
            {
                logger.LogInformation("添加角色权限成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加角色权限失败" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> UpdateRole(UpdateRoleCommand command)
        {
            try
            {
                logger.LogInformation("修改角色权限成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("修改角色权限失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加角色用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddUserRole(AddRoleUserCommand command)
        {
            try
            {
                logger.LogInformation("添加角色用户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("添加角色用户失败" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 修改角色用户
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回结果</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> UpdateUserRole(UpdateUserRoleCommand command)
        {
            try
            {
                logger.LogInformation("修改角色用户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("修改角色用户失败" + ex.Message);
                throw;
            }
        }
    }
}
